package com.bwie.common.exception;

import com.bwie.common.result.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class AllExceptionHandle {
    @ExceptionHandler(Exception.class)
    public Result handle(Throwable throwable){
        //获取处理异常的方法
        StackTraceElement stackTraceElement = throwable.getStackTrace()[0];
        //类名
        String className = stackTraceElement.getClassName();
        //方法名
        String methodName = stackTraceElement.getMethodName();
        //文件名
        String fileName = stackTraceElement.getFileName();
        //报错行数
        int lineNumber = stackTraceElement.getLineNumber();
        return Result.error("发生异常: "+throwable.getMessage()+",报错类名:"+className+",报错方法名:"+methodName+",文件名:"+fileName+",报错行数:"+lineNumber+"");
    }
}
